use ctor::{ctor, dtor};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};

fn main() {
    println!("main start");
    // println!("{}", INITED.load(Ordering::SeqCst));

    // println!("main end")
    panic!("abc");
}

/// 直接初始化
/// This is an immutable static, evaluated at init time
#[ctor]
static STATIC_CTOR: HashMap<u32, &'static str> = {
    println!("STATIC_CTOR");
    let mut m = HashMap::new();
    m.insert(0, "foo");
    m.insert(1, "bar");
    m.insert(2, "baz");
    m
};

static INITED: AtomicBool = AtomicBool::new(false);

// 相当于构造函数，用于初始化
// 会在main主函数之前运行
#[ctor]
fn foo() {
    println!("foo init");
    INITED.store(true, Ordering::SeqCst);
}

/// main函数结束时调用
#[dtor]
unsafe fn shutdown() {
    // Using println or eprintln here will panic as Rust has shut down
    libc::printf("Shutting down!\n\0".as_ptr() as *const i8);

    // println!("Shutting down!");
}
